Stock Exchange Visualizations

in Python


August 21, 2022

Philippe Lambot



In a Snapshot

In this project, I will focus on interactive stock quote visualizations in Python.


This project stands in my GitHub account, with other projects in visualization, for instance in NPS, and in Machine Learning.


Data have been extracted from Yahoo! Finance . They relate to the stocks of eight financial institutions over the period since 2000. Any usage of this document must comply with requirements issued by Yahoo! Finance.

Financial Institutions

The eight financial institutions are listed below with their names and ticker symbols.

Out[2]:
Name of Financial Institution Ticker Symbol
Bank of China Limited BACHY
Barclays PLC BCS
BNP Paribas SA BNPQY
Citigroup Inc. C
Deutsche Bank Aktiengesellschaft DB
ING Groep N.V. ING
JP Morgan Chase & Co. JPM
Mitsubishi UFG Financial Group, Inc. MUFG

As can be seen from the table above, special attention has been paid to the international diversity of the financial institutions chosen. They originate from China, France, Germany, Japan, the Netherlands, the United Kingdom, or the United States. These financial institutions have been picked up using Yahoo! Finance and LexisNexis .

All data consist of stock quotes from Yahoo! Finance . They cover the period from the beginning of 2000 until the end of July 2022. Three institutions have partial data: MUFG (data starting on April 2, 2001), BNPQY (June 5, 2003), and BACHY (December 29, 2009). Stock quotes originate from the NYSE except for BACHY and BNPQY (OTC). They are all denominated in US $.

Closing Prices

The y-axis scale is determined by the sharp drop of Citigroup's stock price. Consequently, let's split the eight lines into three subgroups in accordance with the range of closinng price variations over 20 years. In the first subgroup, we'll keep just Citigroup, in the second subgroup BCS, BNPQY, DB, ING, JPM, and in the third subgroup BACHY and MUFG.


Citigroup

Let's start with Citigroup on its own.

BCS-BNP-DB-ING-JPM

Let's draw line charts for these five financial institutions. Each institution will be dealt with separately but all will have the same y-axis scale to foster comparability.

BACHY-MUFG

And now both financial institutions with lower closing price fluctuations in absolute value: BACHY and MUFG.

BACHY

BACHY is being displayed on the same period.

Extreme Prices

The line charts above have displayed all closing prices. Let's pinpoint here the extreme closing prices throughout the whole period for the eight financial institutions.


Highest Prices

What is the highest closing price for each financial instituion's stock over the whole period?

Out[10]:
Ticker Symbol Highest Closing Price
BACHY 17.990000
BCS 57.566818
BNPQY 63.900002
C 588.750000
DB 152.280533
ING 47.180000
JPM 171.779999
MUFG 16.680000

Lowest Prices

Out[11]:
Ticker Symbol Lowest Closing Price
BACHY 17.990000
BCS 57.566818
BNPQY 63.900002
C 588.750000
DB 152.280533
ING 47.180000
JPM 171.779999
MUFG 16.680000

Close-High-Low Prices

In this section, we'll use data about closing prices, "high" prices, and "low" prices, on a daily basis. These data will be used to build up Bollinger Bands®.


Bollinger Bands®

As stated in Investopedia, "Bollinger Bands® are a technical analysis tool developed by John Bollinger for generating oversold or overbought signals..

The Bollinger Bands® are comprised of three lines:

  • a simple moving average, which is the middle band,
  • an upper band,
  • and lower band.

The simple moving average is typically a 20-day simple moving average of the typical price, that is to say the mean of high price, low price, and closing price of a stock on a daily basis, which will be done in this presentation. It could also be the 20-day simple moving average of the closing price itself. The upper band is typically two standard deviations of the typical price above the middle band. The lower band is typically two standard deviations below the middle band. A candlestick graph can be inserted.

"Many traders believe the closer the prices move to the upper band, the more overbought the market, and the closer the prices move to the lower band, the more oversold the market."

Actually, Bollinger issued 22 rules to follow when using the bands in practice as a trading system.


Bollinger for BNP

Let's draw Bollinger Bands® for BNPQY's stock since June 2022. BNP Paribas SA is the biggest bank in Europe on the basis of assets as of the end of 2020, as shown by InsiderIntelligence.

Bollinger for ING

Let's switch to the Dutch financial institution ING Groep N.V. with Bollinger Bands® since the beginning of 2022.

Returns

A return is the change in price of an asset, investment, or project over time, which may be represented in terms of price change or percentage change. . We will work on returns from stocks expressed in percentage to ensure better comparability between financial institutions.

Extreme Returns

Worst Returns

Out[16]:
Ticker Symbol Date of Closing Price Deepest Drop in %
BACHY 2011-08-08
BCS 2009-01-20
BNPQY 2009-01-20
C 2009-02-27
DB 2009-01-20
ING 2008-10-17
JPM 2009-01-20
MUFG 2008-10-27

January 20, 2019 was [Inauguration Day](https://www.investopedia.com/ask/answers/101314/where-was-dow-jones-when-obama-took-office.asp). How much was the sharpest drop in closing price on 2009-01-20?

Out[17]:
Ticker Symbol Closing Price Drop in % on January 20, 2009
BCS -0.426207
BNPQY -0.244552
DB -0.189406
JPM -0.207274

Best Returns

Out[18]:
Ticker Symbol Date of Strongest Closing Price Increase in %
BACHY 2011-08-11
BCS 2009-01-26
BNPQY 2009-03-10
C 2008-11-24
DB 2008-11-24
ING 2010-05-10
JPM 2009-01-21
MUFG 2003-10-01

Scatter Plot Matrix

In order to retrieve some visual evidence about realtionships between the return time series of the eight financial institutions, let's draw a scatter plot matrix, which is comprised of 56 bilateral scatter plots. For readability purposes, the period has been limited to the third quarter of 2022. Also for readability purposes, the graphs on the diagonal, which could have been histograms, have been removed.

On the basis of the graph above, a relatively high positive correlation can be expected between ING and DB because there are relatively few points in the second and fourth quadrants. A quadrant is very simply defined by the Collins Dictionary as being "any of the four sections into which a plane is divided by two coordinate axes". Points in the second and fourth quadrants point towards negative correlation, points in the first and third quadrants towards positive correlation.


When points are spread in the four quadrants, this points towards low correlation. This looks like it between MUFG and JPM.


We will see that both statements are corroborated by correlation coefficients. But in most cases, the scatter plots above do not give any clear visual indication about correlation because most points are amalgamated and density does not show clearly. Consequently, let's switch to correlation matrices, first over the same period, namely June-August 2022, and then over a much longer period.

Correlation Matrices

June-August 2022

As expected on the basis of some scatter plots, the correlation coefficient between ING and DB is positive and rather important, namely +0.8. On the contrary, the correlation coefficient between MUFG and JPM is rather low, with a value of 0.3, in the line of scatter plots.

Let's switch to a much longer period, namely from 2010 until 2022.

2010-2022

The picture is completely different if we broaden the picture and encompass the period since 2010.

When switching from June-August 2022 to 2010-2022, the correlation coefficient between DB and ING moves from highly positive to slightly negative; there is a similar move between on the one hand DB and on the other hand Citigroup and JPM.


The correlation coefficient between BCS and JPM moves also from 0.8 to -0.6.


The correlation coefficients between BACHY and the other financial institutions become generally positive but do not exceed 0.6.


In a similar way, the correlation coefficients between MUFG and the other financial institutions are rather higher but do not exceed 0.6 except the 0.8 correlation coefficient with ING.